package com.zjt.service.user;

import com.zjt.dao.resource.IReportResourceDao;
import com.zjt.dao.user.IReportDao;
import com.zjt.entity.Report;
import com.zjt.entity.resource.ReportResource;
import com.zjt.utils.DateUtil;
import com.zjt.web.api.vo.base.PageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 物码中心数据采集的DAO interface;
 * 
 * @author xueyanbeijing
 */
@Component
public class ReportService implements IReportService {
    private static Logger logger = LoggerFactory.getLogger(ReportService.class);
    private IReportDao reportDao;
    private IReportResourceDao reportResourceDao;

    @Autowired
    public void setReportResourceDao(IReportResourceDao reportResourceDao) {
        this.reportResourceDao = reportResourceDao;
    }

    @Autowired
    public void setReportDao(IReportDao reportDao) {
        this.reportDao = reportDao;
    }

    @Override
    public Report saveReport(long userId, String address, String lat, String lng, String content) {
        Report report = new Report();
        try
        {
            //TODO 此函数，仅保存记录，不负责保存图片地址
            report.setUserId(userId);
            report.setAddress(address);
            report.setLat(lat);
            report.setLng(lng);
            report.setContent(content);
            report.setCreateDate(DateUtil.getCurrentDate());
            report.setUpdateDate(DateUtil.getCurrentDate());
            report.setEnableFlag("Y");
            report.setType(0);
            report = this.reportDao.save(report);
            logger.info("report insert="+report.toString());
            return report;
        }catch (Exception e)
        {
            return null;
        }
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<Report> getReportListByUserId(long userId) {
        return (List<Report>)this.reportDao.findAll();//用于管理端
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public int countRecordByUserId(long userId) {
        return (int)this.reportDao.count();  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public ReportResource saveReportResource(long reportId, String path) {
        ReportResource reportResource = new ReportResource();
        reportResource.setReportId(reportId);
        reportResource.setPath(path);
        reportResource.setUrlPath(path);
        reportResource.setCreateDate(DateUtil.getCurrentDate());
        reportResource.setUpdateDate(DateUtil.getCurrentDate());
        reportResource.setEnableFlag("Y");
        logger.info("举报资源文件：" + reportResource.toString());
        return this.reportResourceDao.save(reportResource);  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Page<Report> getReportList(PageVO pageVO) {
        PageRequest pageRequest = new PageRequest(pageVO.getPageNo(),pageVO.getPageSize());
        Page<Report> pageList = null;
        pageList = this.reportDao.findAll(pageRequest);
        return pageList;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
